using KeraLua;

namespace Ex
{
    public class ExFixedLuaFunction : ExLuaFunction
    {
        protected ExFixedLuaFunction(EnvLua env, int ref_id) : base(env, ref_id)
        {
        }

        public class A0 : ExFixedLuaFunction
        {

            private A0(EnvLua env, int ref_id) : base(env, ref_id)
            {
            }

            public static A0 MoveTo(ExLuaFunction origin)
            {
                origin.Move(out int ref_id, out EnvLua env);
                return new A0(env, ref_id);
            }

            public void Call()
            {
                lock (env)
                {
                    env.Current.RawGetInteger(LuaRegistry.Index, RefId);
                    var stats = env.Current.PCall(0, 0, 0);
                    if (stats != LuaStatus.OK)
                    {
                        env.Error("call lua function error:" + env.Current.ToString());
                    }
                }
            }
        }

        public class A1<T1>: ExFixedLuaFunction where T1 : notnull 
        {

            private A1(EnvLua env, int ref_id) : base(env, ref_id)
            {
            }

            public static A1<T1> MoveTo(ExLuaFunction origin)
            {
                origin.Move(out int ref_id, out EnvLua env);
                return new A1<T1>(env, ref_id);
            }

            public void Call(T1 t1)
            {
                lock (env)
                {
                    env.Current.RawGetInteger(LuaRegistry.Index, RefId);
                    env.PushObjectToLuaStack(t1);
                    var stats = env.Current.PCall(0, 0, 0);
                    if (stats != LuaStatus.OK)
                    {
                        env.Error("call lua function error:" + env.Current.ToString());
                    }
                }
            }
        }
    }
}